import * as THREE from 'three';
import TWEEN from '../../../node_modules/three/examples/jsm/libs/tween.module.js';

//从点A到点B的动画
//pointA:起点
//pointB:终点
//duration:动画时间 
class lineAnimate{
    constructor(scene,pointA,pointB,duration){
        this.scene = scene;
        this.pointA = pointA;
        this.pointB = pointB;
        this.duration = duration;
        return this.init();
    }
    createLine(){
        this.geometry = new THREE.BufferGeometry();
        this.positions = new Float32Array(2 * 3); // 2个点,每个点xyz坐标
        this.geometry.setAttribute('position', new THREE.BufferAttribute(this.positions, 3));
        // 创建线条材质
        const material = new THREE.LineBasicMaterial({ color: 0xff0000 });
        material.linewidth=3;
        // 创建线条
        const line = new THREE.Line(this.geometry, material);
        this.scene.add(line);
        this.line=line;
    }
    // 在动画更新时更新线条位置
    updateLine(p) {
        this.positions[0] = this.pointA.x;
        this.positions[1] = this.pointA.y; 
        this.positions[2] = this.pointA.z;
        this.positions[3] = p.x;
        this.positions[4] = p.y;
        this.positions[5] = p.z;
        
        this.geometry.attributes.position.needsUpdate = true;
    }
    
    init(){
        this.createLine();
        let currentPoint=this.pointA.clone();
        
        return new TWEEN.Tween(currentPoint)
        .to({
            x: this.pointB.x,
            y: this.pointB.y,
            z: this.pointB.z
        }, this.duration)
        .onUpdate(() => {
            this.updateLine(currentPoint);
        })
        //.start();
    }
        // 创建一个几何体来表示线条
}

export default lineAnimate;
